<?php
// Library File
include 'lib/library.php';

// URL address
$addr = address();

// Local address
$rel_addr = relative_address();

// Database Connection
include $rel_addr.'/lib/mysql_connect.inc';

// Header File
include $rel_addr.'/lib/header.php';

// Footer File
include $rel_addr.'/lib/footer.php';


$content .= '
	<script language="javascript" type="text/javascript">
	
	function ajaxFunction(){
		var ajaxRequest;  // The variable that makes Ajax possible!
		
		try{
			// Opera 8.0+, Firefox, Safari
			ajaxRequest = new XMLHttpRequest();
		} catch (e){
			// Internet Explorer Browsers
			try{
				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try{
					ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e){
					// Something went wrong
					alert("Your browser broke!");
					return false;
				}
			}
		}
		// Create a function that will receive data sent from the server
		ajaxRequest.onreadystatechange = function(){
			if(ajaxRequest.readyState == 4){
				var ajaxDisplay = document.getElementById(\'formcontent\');
				var response = ajaxRequest.responseText;
				var length = response.length;
				
				// Refresh the page is login is successful
				if(length > 380){
					ajaxDisplay.innerHTML = response;
				}else{
					window.location = "'.$addr.'";
				}
			}
		}
		var username = document.getElementById(\'username\').value;
		var password = document.getElementById(\'password\').value;
		var queryString = "?username=" + username + "&password=" + password;
		ajaxRequest.open("GET", "'.$addr.'/handle/login_ajax.php" + queryString, true);
		ajaxRequest.send(null);
	}
	
	function enterKeyPress(e){
		var keycode;
		
		if (window.event) keycode = window.event.keycode;
		else if (e) keycode = e.which;
		
		if (keycode==13) {
			ajaxFunction();
		}
	}
	</script>
';

if(!isset($m_username)){
	$title = 'Login';
}else{
	$title = 'Logout';
}
$subtitle = 'Please Login';

$content .= '
	<div class="formfield">
	
';

if(!isset($m_username)){
	$content .= '
		<div class="formcontent login error" id="formcontent">
		</div>
		<div class="formcontent login">
			<h3>Login</h3>
			<form name="login">
			<P><label for="username">Username: </label><input type="text" id="username" name="username" size="20" onKeyPress="enterKeyPress(event)" /></P>
			<P><label for="password">Password: </label><input type="password" id="password" name="password" size="20" onKeyPress="enterKeyPress(event)" /></P>
			<!--<input type="button" onclick="ajaxFunction()" name="login" class="submit" value="Login" />-->
			<input type="button" onclick="ajaxFunction()" name="login" class="submit" value="Login" />
			</form>
		</div>
	</div>
	';
}else if(isset($m_username)){
	$content = '
		<h2>Refresing</h2>
		<P>You are already logged in...</P>
	';
	refresh('/index.php','none',3);
}

/*
**   P U T    V A R S
**    O N    P A G E
*/
	top($title,$style);
	middle($header,$subtitle,$content);
	bottom($navigation,$footer,$dbc);

?>
